package routers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"user-perm/handlers"
	"user-perm/pkg"
)

func MiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		tokenString := c.Request.Header.Get("token")
		if tokenString == "" {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "token为空",
			})
			c.Abort()
		}
		getToken, err := pkg.ToGetToken(tokenString)
		if err != "" {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "token解析失败",
			})
			c.Abort()
		}
		fmt.Println("解析完token参数后", getToken)
		c.Set("uid", getToken["uid"])
		c.Next()
	}
}
func Init() *gin.Engine {
	r := gin.Default()
	r.POST("/user/login", handlers.UserLogin)
	r.POST("/userAddRole", handlers.UserAddRole)
	r.GET("/userGetRole", handlers.UserGetRole)
	r.POST("/roleAddNode", handlers.RoleAddNode)
	r.GET("/roleGetNode", handlers.RoleGetNode)
	r.GET("/roleList", handlers.RoleList)
	r.GET("/roleDetail", handlers.RoleDetail)
	return r
}
